<?php
/**
 * Created by PhpStorm.
 * User: eugene
 * Date: 2018/5/31
 * Time: 15:01
 */

namespace App\Services;

use GuzzleHttp\Client;
use Illuminate\Support\Facades\Log;

class ScoreService
{
    private $client;
    private $action_code_incre,$action_code_incre_card,$action_code_incre_sign;
    
    public function __construct()
    {
        $config = config('car.api_config');
        $host=$config['score_host'];
        
        $this->action_code_incre = $config['action_code_incre'];
        $this->action_code_incre_sign = $config['action_code_incre_sign'];
        $this->action_code_incre_card = $config['action_code_incre_card'];
        $this->client = new Client(['base_uri'=>$host,'timeout'  => 3.0,'headers'=>[
            'x-client-id'=>'APP',
        ]]);
    }
    
    public function creditsIncreForSign($user_token,$user_id,$credits,$log_id)
    {
        if(env('APP_ENV') == 'local'){
            return true;
        }
        $url = 'point-service/points/burn';
        
        $query = [
            'json'=>[
                'uid'=>$user_id,
                'action'=>$this->action_code_incre_sign,
                'points'=>$credits,
                'description'=>'签到积分',
                'seriesNo'=>md5($log_id),
            ],
        ];
        $response = $this->client->post($url,$query);
    
        if(env('APP_DEBUG'))Log::info('creditsIncreForSign request:'.(string)$response->getBody());
        
        return $response->getStatusCode() == 200;
    }
    
    public function creditsIncre($user_token,$user_id,$credits,$log_id)
    {
        if(env('APP_ENV') == 'local'){
            return true;
        }
        $url = 'point-service/points/burn';
        
        $query = [
            'json'=>[
                'uid'=>$user_id,
                'action'=>$this->action_code_incre,
                'points'=>$credits,
                'description'=>'抽奖积分',
                'seriesNo'=>md5($log_id),
            ],
        ];
        $response = $this->client->post($url,$query);
    
        if(env('APP_DEBUG'))Log::info('creditsIncre request:'.(string)$response->getBody());
        
        return $response->getStatusCode() == 200;
    }
    
    public function creditsIncreCard($user_token,$user_id,$credits,$log_id)
    {
        if(env('APP_ENV') == 'local'){
            return true;
        }
        $url = 'point-service/points/burn';
        
        $query = [
            'json'=>[
                'uid'=>$user_id,
                'action'=>$this->action_code_incre_card,
                'points'=>$credits,
                'description'=>'签到集齐卡片积分',
                'seriesNo'=>md5($log_id),
            ],
        ];
        $response = $this->client->post($url,$query);
    
        if(env('APP_DEBUG'))Log::info('creditsIncreCard request:'.(string)$response->getBody());
        
        return $response->getStatusCode() == 200;
    }
    
    
}